#ifndef __HTTP_DOWNLOAD__h
#define __HTTP_DOWNLOAD__h

#include <ESP8266WiFi.h>

#include <ESP8266HTTPClient.h>
#include <md5.h>
#include "cppstream.h"
#include "timer.h"
//#include "printer.h"
#include "cJSON.h"
#include "Esp.h"
#include "Eboot_command.h"
#include "iot.h"
#include "lwip/dns.h"
#include "ESP8266WiFiGeneric.h"



#define DOWNJSON 1
#define DOWNBIN  2
#define WDT 0x40002fa0
#define ONESECTORSIZE  0x1000
#define ENDADDRESS     0x100000
#define NOWVERSION     1
#define UPDATE_LED 14
#define UPDATE_BUTTON 5

typedef void (*voidFunc)(void);

extern const char* DFJsonUrl;
extern const char* TestJsonUrl;

extern String jsonBinMD5;
extern char *jsonBinFileUrl;
extern int jsonBinVersion;
//extern JsonData * jsondata;

int httpGetJson(WiFiClient& client);
//int httpGetJson(WiFiClient& client,int platform,char* platformVersion);
int jsonParse(char *parsemsg);
bool downJson(WiFiClient& client);
bool downBin(WiFiClient& client);
bool readMD5(size_t fsz,int sectornum);
void findNewVersion();
void noNewVersion();
bool platformDownBin();

void checkModuleReport(uint8_t cmd,void *buf,uint8_t len,bool isString,uint8_t cs);
void setColor(int color);

String uint8ToString(uint8_t* buff);
String typechange(int a);

#define CMD_CONNECT_WIFI        0x01
#define CMD_DISCONNECT_WIFI     0x02
#define CMD_LOG_IN              0x03
#define CMD_LOG_OUT             0x04
#define CMD_SET_SERVER          0x05
#define CMD_SUBSCRIBE           0x06
#define CMD_PUBLISH             0x07
#define CMD_UPGRADE_FW          0x08
/**Multiple platforms**/
#define PLATFORM_CURRENT        0X01 
#define PLATFORM_DEFAULT        0x00
#define PLATFORM_DFROBOT        0x01
#define PLATFORM_AZURE          0x02
#define PLATFORM_AWS            0x03
extern int platformReady;
extern char platformVersionReady[10];
#define VERSION_DEFAULT         "0" //The latest
/****/

#define CMD_MODULE_REPORT       0x10
#define CMD_MODULE_SUB_DATA     0x11
#define CMD_MODULE_CONNECT      0x12

#define CMD_QUERY_WIFI          0x20
#define CMD_QUERY_IP            0x21
#define CMD_QUERY_SERVER        0x22

#define CMD_RESET               0x30

#define CMD_GET_VERSION         0x40

#define CMD_RSPMASK             0x80

#define EVENT_CODE_WIFI           'W'
#define EVENT_CODE_SERVER         'S'
#define EVENT_CODE_NEW_VERSION    'V'
#define EVENT_CODE_UPDATE_PERCENT 'P'//down %

#define EVENT_CODE_DISCONNECT     2
#define EVENT_CODE_CONNECT        1
#define EVENT_CODE_ERR            0
#define EVENT_CODE_VERSION_YES    1
#define EVENT_CODE_VERSION_NO     0

#define REDLED                    4//GPIO 4 PWM3
#define BLUELED                   12//gpio 12 pwm0
#define GREENLED                  14//gpio 14 pwm2

void sendUpdatePercent(uint8_t percent);
#define JHDBG(...) if(0){Serial.printf("[%s():%d] ",__FUNCTION__,__LINE__);Serial.printf(__VA_ARGS__);}

typedef struct{
  uint8_t header55;
  uint8_t headeraa;
  uint8_t frameNo;
  uint8_t length; //不包含cs
  uint8_t cmd;
  uint8_t payload[0];
}__attribute__ ((packed)) tPacketHeader,*pPacketHeader;

#endif
